<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
require('echarts/theme/macarons')
import resize from '@/components/Charts/mixins/resize'

export default {
  name: 'LineChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      this.getData()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el, 'macarons')
      const initOption = {
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(255,255,255,0.8)', // 设置背景图片 rgba格式
          color: 'black',
          borderWidth: '1', // 边框宽度设置1
          borderColor: 'gray', // 设置边框颜色
          textStyle: {
            color: 'black' // 设置文字颜色
          }
        },
        legend: {
          data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true // 在计算这些距离时是否加入标签
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            // 同个类目轴上配置了相同的stack值后，后一个系列的值就会在前一个的值上相加
            name: '邮件营销',
            type: 'line',
            // stack: '总量',
            // 这个主题默认smooth为true
            smooth: false
          },
          {
            name: '联盟广告',
            type: 'line',
            // stack: '总量',
            smooth: false
          },
          {
            name: '视频广告',
            type: 'line',
            // stack: '总量',
            smooth: false
          },
          {
            name: '直接访问',
            type: 'line',
            // stack: '总量',
            smooth: false
          },
          {
            name: '搜索引擎',
            type: 'line',
            // stack: '总量',
            smooth: false
          }
        ]
      }
      this.chart.setOption(initOption)
    },
    getData() {
      this.updateChart()
    },
    updateChart() {
      const data1 = [120, 132, 101, 134, 90, 230, 210]
      const data2 = [220, 182, 191, 234, 290, 330, 310]
      const data3 = [150, 232, 201, 154, 190, 330, 410]
      const data4 = [320, 332, 301, 334, 390, 330, 320]
      const data5 = [820, 932, 901, 934, 1290, 1330, 1320]
      const updateOption = {
        series: [
          { data: data1 },
          { data: data2 },
          { data: data3 },
          { data: data4 },
          { data: data5 }
        ]
      }
      this.chart.setOption(updateOption)
    }
  }
}
</script>

<style scoped>

</style>
